using Microsoft.AspNetCore.Mvc;
using RAG.Application.Services.Interfaces;
using RAG.Application.Dtos.SystemLog;

namespace RAG.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SystemLogController : ControllerBase
{
    private readonly ISystemLogService _systemLogService;

    public SystemLogController(ISystemLogService systemLogService)
    {
        _systemLogService = systemLogService;
    }

    /// <summary>
    /// 创建系统日志
    /// </summary>
    [HttpPost]
    public async Task<ActionResult<SystemLogDto>> CreateSystemLog([FromBody] SystemLogDto systemLogDto)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var createdLog = await _systemLogService.CreateSystemLogAsync(systemLogDto);
            return CreatedAtAction(nameof(GetAllSystemLogs), createdLog);
        }
        catch (InvalidOperationException ex)
        {
            return BadRequest(new { message = ex.Message });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "创建系统日志失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 获取所有系统日志（按创建时间排序）
    /// </summary>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<SystemLogDto>>> GetAllSystemLogs()
    {
        try
        {
            var logs = await _systemLogService.GetAllSystemLogsAsync();
            return Ok(logs);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "获取系统日志列表失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 根据操作类型查询系统日志
    /// </summary>
    [HttpGet("action-type/{actionType}")]
    public async Task<ActionResult<IEnumerable<SystemLogDto>>> GetSystemLogsByActionType(string actionType)
    {
        try
        {
            var logs = await _systemLogService.GetSystemLogsByActionTypeAsync(actionType);
            return Ok(logs);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "根据操作类型查询系统日志失败", error = ex.Message });
        }
    }
} 